package com.malou.client.ventas.modeloprenda;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FormEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.ProgressBar;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.layout.CardLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.malou.client.MalouConstants;

public class UploadFilePanel extends ContentPanel {

	private MalouConstants constants = GWT.create(MalouConstants.class);
	private FormPanel panelFile;	
	private FormPanel formPanel;
	private TextField<String> imageName;
	private HiddenField<String> imageNameThumb;
	private Timer t;
	private FileUploadField file;
	private CardLayout panelLayout;
	
	public UploadFilePanel(){
		setFrame(true);
        setHeaderVisible(false);
        setHeight(110);
        panelLayout = new CardLayout(); 
        setLayout(panelLayout);
        setButtonAlign(HorizontalAlignment.CENTER);
        
        panelFile = new FormPanel();
        panelFile.setLabelAlign(LabelAlign.TOP);
        panelFile.setHeaderVisible(false);
        
        panelFile.setFrame(false);  
        panelFile.setAction(GWT.getModuleBaseURL() + "UploadFile");  
        panelFile.setEncoding(Encoding.MULTIPART);  
        panelFile.setMethod(Method.POST);
        
        panelFile.addListener(Events.Submit, new Listener<FormEvent>() {

			@Override
			public void handleEvent(FormEvent be) {				
				fireEvent(Events.Submit, be);
				imageNameThumb.setValue(be.getResultHtml());
				panelLayout.setActiveItem(formPanel);				
			}
		});
        
        file = new FileUploadField();  
        file.setAllowBlank(true);  
        file.setName("uploadedfile");        
        file.setFieldLabel(constants.cambiarImagen());  
        panelFile.add(file);
        final String messageFile = constants.formatoNoValido();
        file.setValidator(new Validator() {
			
			@Override
			public String validate(Field<?> field, String value) {
				int dot = value.lastIndexOf(".");
				String extension = value.substring(dot+1).toUpperCase();
				if(!(extension.equals("JPG") || extension.equals("JPEG") || extension.equals("PNG") || extension.equals("GIF") || extension.equals("TIFF") || extension.equals("TIF")))
					return messageFile;
				return null;
			}
		});
        
        file.addListener(Events.OnChange, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
				if(file.isValid())
				if(file.getValue() != null){
	        		  panelFile.submit();
	        		  panelLayout.setActiveItem(panelFile);
	        		  t.scheduleRepeating(500);
	        		  String value = file.getValue();
	        		  int barra = value.lastIndexOf("\\");
	        		  String name = value.substring(barra + 1);
	        		  imageName.setValue(name);	        		  
	        	  }				
			}
		});

        
        
        
        final ProgressBar bar = new ProgressBar();
        t = new Timer() {
            float i;

            @Override
            public void run() {
              bar.updateProgress(i / 100, (int) i + "% Complete");
              i += 5;
              if (i > 105) {
                cancel();                       
              }
            }
          };
        //t.scheduleRepeating(500);
          
        
        Label lbar = new Label("Cargando...");        
        lbar.setStyleAttribute("fontSize", "12px");
        lbar.setStyleAttribute("padding", "3px 3px 3px 0");        
        
        add(panelFile);
        
        final ContentPanel panelBar = new ContentPanel();
        panelBar.addStyleName("x-window-dlg");
        panelBar.setStyleAttribute("padding", "10px");
        panelBar.setHeaderVisible(false);
        
        panelBar.setFrame(false);
        panelBar.add(lbar);
        
        panelBar.add(bar);
        add(panelBar);
        
        formPanel = new FormPanel();
        formPanel.setHeaderVisible(false);
        formPanel.setFrame(false);
        formPanel.setLabelAlign(LabelAlign.TOP);
        
        imageName = new TextField<String>();
        imageName.setFieldLabel(constants.imagen());
        imageName.setReadOnly(true);
        formPanel.add(imageName);
        
        imageNameThumb = new HiddenField<String>();
        formPanel.add(imageNameThumb);
        
        add(formPanel);
        
        Button btn = new Button(constants.guardar());  
        btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
          @Override  
          public void componentSelected(ButtonEvent ce) {
        	  if(imageName.getValue() != null && imageNameThumb.getValue() != null){
	        	    UploadFileEvent ue = new UploadFileEvent(UploadFilePanel.this);
	        	    ue.setImageName(imageName.getValue());
	        	    ue.setImageNameThumb(imageNameThumb.getValue());
	        	    fireEvent(UploadFileEvents.SaveData, ue);
        	    }
          }  
        });  
        addButton(btn);  
      
        btn = new Button(constants.cancelar());  
        btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
          @Override  
          public void componentSelected(ButtonEvent ce) {
        	  clear();
        	  fireEvent(UploadFileEvents.Cancel);
          }  
        });
        
        addButton(btn);
        
	}
	public void clear(){
	  imageName.clear();
  	  imageNameThumb.clear();  	
  	  file.reset();
  	  panelLayout.setActiveItem(panelFile);
	}
	protected ComponentEvent createComponentEvent(Event event){
		return new UploadFileEvent(this);
	}
}
